/*
 * @Date: 2021-11-16 17:09:36
 * @Version: 0.0.1
 * @Author: chenhengling
 * @Description: 工具方法
 * @LastEditTime: 2021-11-17 13:50:53
 * @LastEditors: chenhengling
 */
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');

/**
 * @description: 封装log
 * @param {*} message
 * @param {*} success
 * @return {*}
 */
function log(message, success = false) {
  console.log(chalk[success ? 'green' : 'red'](message));
}

/**
 * @description: 删除文件
 * @param {*} filepath
 * @return {*}
 */
function deleteFile(filepath) {
  if (fs.existsSync(filepath)) {
    if (fs.statSync(filepath).isDirectory()) {
      // 删除文件夹
      const files = fs.readdirSync(filepath);
      files.forEach((file) => {
        const _filepath = path.join(filepath, file);
        deleteFile(_filepath);
      });
      fs.rmdirSync(filepath);
    } else {
      // 删除文件
      fs.unlinkSync(filepath);
    }
  }
}

module.exports = {
  log,
  deleteFile,
};
